package com.xdcplus.web.service.bd.impl;

import cn.hutool.core.util.StrUtil;
import com.xdcplus.mom.constants.NumberConstant;
import com.xdcplus.mom.enums.ResponseEnum;
import com.xdcplus.exceptions.XdcWebException;
import java.util.Arrays;
import cn.hutool.core.collection.CollectionUtil;

import com.xdcplus.mp.service.impl.BaseServiceImpl;
import com.xdcplus.web.mapper.bd.BdWetWeightTargetMapper;
import com.xdcplus.web.common.pojo.entity.bd.BdWetWeightTarget;
import com.xdcplus.web.common.pojo.dto.bd.BdWetWeightTargetDTO;
import com.xdcplus.web.common.pojo.dto.bd.BdWetWeightTargetFilterDTO;
import com.xdcplus.web.common.pojo.vo.bd.BdWetWeightTargetVO;
import com.xdcplus.web.common.pojo.query.bd.BdWetWeightTargetQuery;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageInfo;
import com.xdcplus.core.utils.BeanUtils;
import com.xdcplus.pager.vo.PageVO;
import com.xdcplus.mp.utils.PageableUtils;
import com.xdcplus.mp.utils.PropertyUtils;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.sql.Timestamp;

import com.xdcplus.web.service.bd.BdWetWeightTargetService;
import org.springframework.stereotype.Service;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;

/**
 * 丝印湿重目标维护(BdWetWeightTarget)表服务实现类
 *
 * @author Zhecheng.Ren
 * @since 2024-10-22 15:10:15
 */
@Slf4j
@Service("bdWetWeightTargetService")
public class BdWetWeightTargetServiceImpl extends BaseServiceImpl<BdWetWeightTargetMapper, BdWetWeightTarget, BdWetWeightTarget, BdWetWeightTargetVO> implements BdWetWeightTargetService {

    @Resource
    protected BdWetWeightTargetMapper bdWetWeightTargetMapper;

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void saveBdWetWeightTarget(BdWetWeightTargetDTO bdWetWeightTargetDTO) {
        String bayCode = bdWetWeightTargetDTO.getBayCode();
        Integer roadNum = bdWetWeightTargetDTO.getRoadNum();
        if (StrUtil.isBlank(bayCode) || ObjectUtil.isNull(roadNum)) {
            throw new XdcWebException(ResponseEnum.ERROR, "线别和道次不能为空");
        }
        BdWetWeightTarget weightTarget = bdWetWeightTargetMapper.queryByBayCodeAndRoadNum(bayCode, roadNum);
        if (ObjectUtil.isNotNull(weightTarget)) {
            throw new XdcWebException(ResponseEnum.ERROR, "此线别道次已维护目标值");
        }
        BdWetWeightTarget bdWetWeightTarget = new BdWetWeightTarget();
        BeanUtil.copyProperties(bdWetWeightTargetDTO, bdWetWeightTarget);
        bdWetWeightTarget.setCreatedTime(new Timestamp(DateUtil.current()));

        this.save(bdWetWeightTarget);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void updateBdWetWeightTarget(BdWetWeightTargetDTO bdWetWeightTargetDTO) {
        String bayCode = bdWetWeightTargetDTO.getBayCode();
        Integer roadNum = bdWetWeightTargetDTO.getRoadNum();
        Long id = bdWetWeightTargetDTO.getId();
        if (StrUtil.isBlank(bayCode) || ObjectUtil.isNull(roadNum)) {
            throw new XdcWebException(ResponseEnum.ERROR, "线别和道次不能为空");
        }
        BdWetWeightTarget wetWeightTarget = bdWetWeightTargetMapper.queryByBayCodeAndRoadNumAndIdNot(bayCode, roadNum, id);
        if (ObjectUtil.isNotNull(wetWeightTarget)) {
            throw new XdcWebException(ResponseEnum.ERROR, "此线别道次已维护目标值");
        }
        BdWetWeightTarget bdWetWeightTarget = this.getById(id);
        if (ObjectUtil.isNull(bdWetWeightTarget)) {
            log.error("updateBdWetWeightTarget() The BdWetWeightTarget does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }

        BeanUtil.copyProperties(bdWetWeightTargetDTO, bdWetWeightTarget);
        bdWetWeightTarget.setUpdatedUser(bdWetWeightTargetDTO.getUpdatedUser());
        bdWetWeightTarget.setUpdatedTime(new Timestamp(DateUtil.current()));

        this.updateById(bdWetWeightTarget);
    }

    @Override
    public Boolean saveOrUpdateBatch(List<BdWetWeightTarget> bdWetWeightTargetList) {

        bdWetWeightTargetList.forEach(bdWetWeightTarget -> {
            BdWetWeightTarget bdWetWeightTargetParam = new BdWetWeightTarget();
            bdWetWeightTargetParam.setId(bdWetWeightTarget.getId());
            if (ObjectUtil.isNotNull(bdWetWeightTarget.getId())) {
                bdWetWeightTarget.setId(bdWetWeightTarget.getId());
                bdWetWeightTarget.setUpdatedTime(new Timestamp(DateUtil.current()));
                LambdaUpdateWrapper<BdWetWeightTarget> lambdaUpdate = Wrappers.lambdaUpdate();
                lambdaUpdate.eq(BdWetWeightTarget::getId, bdWetWeightTarget.getId());
                update(bdWetWeightTarget, lambdaUpdate);
            } else {
                bdWetWeightTarget.setCreatedTime(new Timestamp(DateUtil.current()));
                save(bdWetWeightTarget);
            }
        });
        return true;
    }

    @Override
    public Boolean saveOrUpdateBatchByDTOList(List<BdWetWeightTargetDTO> bdWetWeightTargetDTOList) {

        List<BdWetWeightTarget> bdWetWeightTargetList = BeanUtils.copyProperties(bdWetWeightTargetDTOList, BdWetWeightTarget.class);
        return saveOrUpdateBatch(bdWetWeightTargetList);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteBdWetWeightTargetLogical(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        BdWetWeightTarget bdWetWeightTarget = this.getById(id);

        if (ObjectUtil.isNull(bdWetWeightTarget)) {
            log.error("deleteBdWetWeightTarget() The BdWetWeightTarget does not exist or has been deleted");
            throw new XdcWebException(ResponseEnum.ERROR);
        }
        this.removeById(id);
    }
    
    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void deleteBdWetWeightTargetByIds(String ids) {
        Assert.notNull(ids, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());
		List<String> idList = Arrays.asList(ids.split(","));
		if(CollectionUtil.isEmpty(idList)){
           throw new XdcWebException(ResponseEnum.ERROR);
       }
		idList.forEach(id->{
			BdWetWeightTarget bdWetWeightTarget = this.getById(id);
			if (ObjectUtil.isNull(bdWetWeightTarget)) {
				log.error("deleteBdWetWeightTarget() The BdWetWeightTarget does not exist or has been deleted");
				throw new XdcWebException(ResponseEnum.ERROR);
			}
		});

        this.removeByIds(idList);
    }

    private List<BdWetWeightTarget> queryBdWetWeightTargetList(BdWetWeightTargetFilterDTO bdWetWeightTargetFilterDTO) {
        BdWetWeightTargetQuery bdWetWeightTargetQuery = BeanUtil.copyProperties(bdWetWeightTargetFilterDTO, BdWetWeightTargetQuery.class);

        return bdWetWeightTargetMapper.queryBdWetWeightTarget(bdWetWeightTargetQuery);
    }

    @Override
    public List<BdWetWeightTargetVO> queryBdWetWeightTargetVOList(BdWetWeightTargetFilterDTO bdWetWeightTargetFilterDTO) {
        return this.objectConversion(queryBdWetWeightTargetList(bdWetWeightTargetFilterDTO));
    }

    @Override
    public PageVO<BdWetWeightTargetVO> queryBdWetWeightTarget(BdWetWeightTargetFilterDTO bdWetWeightTargetFilterDTO) {
        PageVO<BdWetWeightTargetVO> pageVO = new PageVO<>();

        if (bdWetWeightTargetFilterDTO.getCurrentPage() > NumberConstant.ZERO) {
            PageableUtils.basicPage(bdWetWeightTargetFilterDTO.getCurrentPage(), bdWetWeightTargetFilterDTO.getPageSize(),
                    bdWetWeightTargetFilterDTO.getOrderType(), bdWetWeightTargetFilterDTO.getOrderField());
        }

        List<BdWetWeightTarget> bdWetWeightTargetList = queryBdWetWeightTargetList(bdWetWeightTargetFilterDTO);

        PageInfo<BdWetWeightTarget> pageInfo = new PageInfo<>(bdWetWeightTargetList);
        PropertyUtils.copyProperties(pageInfo, pageVO, this.objectConversion(bdWetWeightTargetList));

        return pageVO;
    }

    @Override
    public BdWetWeightTargetVO queryBdWetWeightTargetById(Long id) {

        Assert.notNull(id, ResponseEnum.THE_ID_CANNOT_BE_EMPTY.getMessage());

        return this.objectConversion(this.getById(id));
    }
}
